//
//  SignAgreementController.m
//  LuCaiZhu
//
//  Created by stella Ma on 2018/9/26.
//
#import "Header.h"
#import "UIColor+NN.h"
#import "CKPuslichTool.h"
#import "SVProgressHUD.h"
#import "HandleNetProduct.h"
#import "TKAlertCenter.h"
#import "VerifySMSController.h"
#import "UIImageView+WebCache.h"
#import "SignAgreementController.h"

@interface SignAgreementController ()<UIWebViewDelegate>{
    NSString *bookingId;
    NSString *stepName;
}
@property (weak, nonatomic) IBOutlet UILabel *name;
@property (weak, nonatomic) IBOutlet UILabel *cardType;
@property (weak, nonatomic) IBOutlet UILabel *cardNo;
@property (weak, nonatomic) IBOutlet UIButton *submitBt;
@property (weak, nonatomic) IBOutlet UIImageView *logoPic;
@property (weak, nonatomic) IBOutlet UIWebView *mWebView;
@property (weak, nonatomic) IBOutlet UIButton *iagreeBt;
@property (weak, nonatomic) IBOutlet UILabel *mTextLabel;

@property (weak, nonatomic) IBOutlet UILabel *company;
@property (weak, nonatomic) IBOutlet UILabel *signTime;


@end

@implementation SignAgreementController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"电子签名约定书";
    [[NSURLCache sharedURLCache] removeAllCachedResponses];
    UIViewSetRadius(_submitBt, 22, 0, nil);
    UIViewSetRadius(_logoPic, 50, 0, nil);
    NSLog(@"self.stepDic---%@",self.stepDic);
    bookingId = _stepDic[@"BookingId"];
    stepName = _stepDic[@"StepName"];
    [self getMyDatas];
    
    if ([self.stepDic[@"Status"] isEqualToString:@"完成"]) {
        self.submitBt.hidden = YES;
        
    }else{
        self.submitBt.hidden = NO;
    }
    
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; paragraphStyle.lineSpacing = 8;// 字体的行间距
    paragraphStyle.paragraphSpacing = 12;
    NSDictionary *attributes = @{ NSFontAttributeName:[UIFont systemFontOfSize:15], NSParagraphStyleAttributeName:paragraphStyle };
    _mTextLabel.attributedText = [[NSAttributedString alloc] initWithString:_mTextLabel.text attributes:attributes];
    
    _mTextLabel.textColor = [UIColor colorWithHexString:@"676869"];
    
}

//- (void)loadPDFFiles:(NSString *)urlString{
//    NSCharacterSet *encodeUrlSet = [NSCharacterSet URLQueryAllowedCharacterSet];
//    NSString *encodeUrl = [urlString stringByAddingPercentEncodingWithAllowedCharacters:encodeUrlSet];
//    NSLog(@"encodeUrl---%@",encodeUrl);
//    NSURL *url = [NSURL URLWithString:encodeUrl];
//    
//    [HandleNetProduct show];
//    _mWebView.delegate = self;
//    NSURLRequest *request = [NSURLRequest requestWithURL:url];
//    [self.mWebView loadRequest:request];
//}

- (void)getMyDatas{
    [HandleNetProduct getSignatureAgreementforBookingID:bookingId Success:^(NSDictionary *data) {
        _name.text = data[@"AccountName"];
        _cardType.text = data[@"IDDocumenttype"];
        _cardNo.text = data[@"IDNumber"];
        _company.text = data[@"SalesOrganization"];
        _signTime.text = data[@"Signdate"];
         [_logoPic sd_cancelCurrentImageLoad];
        NSData *picData = [NSData dataWithContentsOfURL:[NSURL URLWithString:data[@"ElectronicSeal"]]];
        [_logoPic setImage:[UIImage imageWithData:picData]];
       
//        [self loadPDFFiles:data[@"QMUrl"]];
    } Failure:^(NSString *errorInfo) {
        
    }];
}

- (IBAction)iagree:(UIButton *)sender {
    sender.selected = !sender.isSelected;
}

- (IBAction)submit:(id)sender {
    if (!_iagreeBt.selected) {
        [[TKAlertCenter defaultCenter]postAlertWithMessage:@"请先勾选同意《电子签名约定书》"];
        return;
    }
    
    if ([_stepDic[@"IsSMS"] isEqualToString:@"Yes"]) {
        //判断改为如果是最后一步
        VerifySMSController *vc = (VerifySMSController *)[CKPuslichTool getViewControllerFromStroyBoard:@"My" viewID:@"VerifySMSController"];
        [vc setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
        [vc setModalPresentationStyle:UIModalPresentationOverFullScreen];
        vc.returnVerifyOK = ^{
            [self okPress];
        };
        vc.isEmail = NO;
        
        [self presentViewController:vc animated:YES completion:nil];
    }else{
        [self okPress];
    }
    
    
}

- (void)okPress{
    _submitBt.enabled = NO;
    [HandleNetProduct updateSigningStatusforBookingID:bookingId SignName:stepName Success:^(NSDictionary *data) {
        _submitBt.enabled = YES;
        if (self.returnSubmitOK) {
            self.returnSubmitOK();
        }
        [self.navigationController popViewControllerAnimated:YES];
    } Failure:^(NSString *errorInfo) {
        _submitBt.enabled = YES;
    }];
}

//- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationTyp{
//
//
//    return YES;
//
//}
//
//- (void)webViewDidStartLoad:(UIWebView *)webView{
//    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
//}
//- (void)webViewDidFinishLoad:(UIWebView *)webView{
//    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
//    [SVProgressHUD dismiss];
//
//
//}
//
//- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
//
//
//    [SVProgressHUD dismiss];
//    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
//
//}

#pragma mark - Table view data source
//
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 3;
    }
    return 1;
}
- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 2) {
        return UITableViewAutomaticDimension;
    }
    if (indexPath.section == 1 && indexPath.row == 0) {
        
        if ([self.stepDic[@"Status"] isEqualToString:@"完成"]) {
            
            return 98;
        }
        return 150;
    }
    return 45;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 0.01f;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
